package com.xyf.sys.service.impl;

import com.xyf.sys.domain.SysProduct;
import com.xyf.sys.mapper.SysProductMapper;
import com.xyf.sys.service.IProductService;
import com.xyf.sys.vo.ProductListRequestVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Author R-H
 * @Date 2024/4/151706
 * @Description
 */
@Service
@Slf4j
public class ProductServiceImpl implements IProductService {

    @Autowired
    private SysProductMapper productMapper;

    @Override
    public List<SysProduct> list(ProductListRequestVo body) {
        if (body.getCategoryId() != null && body.getCategoryId() == 0L){
            body.setCategoryId(null);
        }
        body.setStatus("1");
        body.setState("1");
        return productMapper.selectList(body);
    }

    @Override
    public SysProduct get(Long id) {
        return productMapper.selectByPrimaryKey(id);
    }

    @Override
    public Object add(SysProduct product) {
        product.setDelFlag("0");
        product.setStatus((byte)1);
        product.setState((byte)1);
        product.setSort(productMapper.count() + 1);
        product.setCreateBy(product.getUserId());
        product.setCreateTime(new Date());
        return productMapper.insert(product);
    }

    @Override
    public Object up(Long id) {
        SysProduct product = productMapper.selectByPrimaryKey(id);
        product.setStatus((byte)1);
        product.setUpdateBy(product.getUserId());
        product.setUpdateTime(new Date());
        return productMapper.updateByPrimaryKey(product);
    }

    @Override
    public Object down(Long id) {
        SysProduct product = productMapper.selectByPrimaryKey(id);
        product.setStatus((byte) -1);
        product.setUpdateBy(product.getUserId());
        product.setUpdateTime(new Date());
        return productMapper.updateByPrimaryKey(product);
    }
}

